<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        font-family: sans-serif;
      }

      #container {
        font-size: 20px;
        line-height: 30px;
        max-width: 600px;
      }

      #bg-image.lazy {
        background-image: none;
        background-color: #f1f1fa;
      }
      #bg-image {
        background-image: url('https://ik.imagekit.io/demo/img/image10.jpeg?tr=w-600,h-400');
        max-width: 600px;
        height: 400px;
      }
    </style>
  </head>
  <body>
    <div id="container">
      <h3>Lazy loading CSS background images</h3>
      <img
      style="filter: blur(250);"
        width="250"
        height="250"
        src=""
        data-src="https://s-media-cache-ak0.pinimg.com/474x/50/1b/74/501b74902935b063816ea8e14f460ca0.jpg"
        alt="Ghost In The Shell"
      />
      <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam at
        ligula non lorem facilisis finibus. Suspendisse turpis lacus, tempus sit
        amet dignissim sit amet, pretium non sem. Ut malesuada purus ac magna
        placerat, tempus commodo turpis consectetur. Nunc vitae metus vehicula
        nunc congue varius. Suspendisse egestas urna non finibus feugiat.
        Vestibulum maximus, nunc et tempus convallis, dui ante vestibulum est,
        in lobortis purus sapien eget lacus. Suspendisse cursus enim non quam
        mollis imperdiet. Pellentesque convallis eget nunc vitae fringilla.
        Fusce interdum, velit semper varius aliquet, eros sapien facilisis erat,
        iaculis vulputate erat arcu vel neque. Suspendisse dignissim hendrerit
        lorem. Etiam metus ex, finibus vitae iaculis vitae, iaculis non ex.
        Phasellus commodo id ante id consequat. Nunc consectetur viverra lacus
        non fringilla. Pellentesque nec nisl vulputate, sagittis nisl id,
        venenatis nisi. Nunc malesuada tellus a elit suscipit tempor.
      </p>
      <p>
        Nam lacinia tortor quis volutpat lacinia. Aliquam in orci in nunc
        vehicula maximus. Phasellus elementum nulla augue, at aliquam sem
        pulvinar dapibus. Vivamus molestie venenatis risus pulvinar interdum.
        Phasellus blandit tortor eget nulla sagittis auctor. Cras sed leo in
        velit lobortis euismod. Suspendisse non ante tellus.
      </p>
      <div id="bg-image" class="lazy"></div>
      <p>
        Nam lacinia tortor quis volutpat lacinia. Aliquam in orci in nunc
        vehicula maximus. Phasellus elementum nulla augue, at aliquam sem
        pulvinar dapibus. Vivamus molestie venenatis risus pulvinar interdum.
        Phasellus blandit tortor eget nulla sagittis auctor. Cras sed leo in
        velit lobortis euismod. Suspendisse non ante tellus.
      </p>
    </div>
    <script>
      document.addEventListener('DOMContentLoaded', function () {
        var lazyloadImages

        if ('IntersectionObserver' in window) {
          lazyloadImages = document.querySelectorAll('.lazy')
          var imageObserver = new IntersectionObserver(function (
            entries,
            observer
          ) {
            entries.forEach(function (entry) {
              if (entry.isIntersecting) {
                var image = entry.target
                image.classList.remove('lazy')
                imageObserver.unobserve(image)
              }
            })
          })

          lazyloadImages.forEach(function (image) {
            imageObserver.observe(image)
          })
        } else {
          var lazyloadThrottleTimeout
          lazyloadImages = document.querySelectorAll('.lazy')

          function lazyload() {
            if (lazyloadThrottleTimeout) {
              clearTimeout(lazyloadThrottleTimeout)
            }

            lazyloadThrottleTimeout = setTimeout(function () {
              var scrollTop = window.pageYOffset
              lazyloadImages.forEach(function (img) {
                if (img.offsetTop < window.innerHeight + scrollTop) {
                  img.src = img.dataset.src
                  img.classList.remove('lazy')
                }
              })
              if (lazyloadImages.length == 0) {
                document.removeEventListener('scroll', lazyload)
                window.removeEventListener('resize', lazyload)
                window.removeEventListener('orientationChange', lazyload)
              }
            }, 20)
          }

          document.addEventListener('scroll', lazyload)
          window.addEventListener('resize', lazyload)
          window.addEventListener('orientationChange', lazyload)
        }
      })
    </script>
  </body>
</html>
